package com.chinaway.dsp.conn;

import java.lang.reflect.Field;
import org.apache.commons.codec.binary.Hex;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import com.chinaway.dsp.action.BackAction;
import com.chinaway.dsp.action.IteratorSendAction;
import com.chinaway.dsp.test.MessageSend;
import com.chinaway.dsp.util.CountBean;

public class ServerResponse extends IoHandlerAdapter{
	public String rep="";
	private Display display;
	private Shell shell;
	private Button connbtn;
	private Button send_stop_btn;
	private Text log_info;
	private CountBean cb;
	private BackAction backAction;
	private IteratorSendAction iteratorSendAction;
	private Button send_iteration_btn;
	
	public ServerResponse(Display d,Shell s,Button b,Text t,CountBean c,Button b1,Button b2) {
		this.shell=s;
		this.display=d;
		this.connbtn=b;
		this.log_info=t;
		this.cb=c;
		this.send_stop_btn=b1;
		this.send_iteration_btn=b2;
	}
	
	public void setBa(BackAction ba){
		this.backAction=ba;
	}
	
	public void setIsa(IteratorSendAction isa){
		this.iteratorSendAction=isa;
	}

	@Override
	public void messageReceived(IoSession session, Object message)throws Exception {
		byte[] byArr = (byte[]) message;
		char[] chArr = Hex.encodeHex(byArr);
		String str="";
		for (char c : chArr) {
			str+=String.valueOf(c).toUpperCase();
		}
		String respone="";
		for(int i=0;i<str.length();i++){
			if(i%2==0){
				String tmp=str.substring(i,i+2);
				respone=respone+" "+tmp;
			}
		}
		respone=respone.substring(1,respone.length());
		rep=respone;
		display.syncExec(new Runnable() {
            public void run() {
            	log_info.append("响应："+rep+"\n");
            	
            }});
	}
	
	@Override
	public void sessionClosed(IoSession session) throws Exception {
		
		display.syncExec(new Runnable() {
            public void run() {
            	if(!"连接".equals(connbtn.getText())){
            		//cb.connstatus=false;
            		backAction.setSendString("stop");
            		if(null!=iteratorSendAction)
            			iteratorSendAction.destroyThread=true;
            		send_iteration_btn.setText("连续发");
            			if(cb.count>0)
                			send_stop_btn.setText("继续");
            		MessageBox msgBox = new MessageBox( shell, SWT.OK | SWT.ICON_ERROR );
				    msgBox.setText( "错误!" );
				    msgBox.setMessage( "服务器已断开,请重连!" );
				    msgBox.open();
            		connbtn.setText("重新连接");
            		log_info.append("连接已断开,请重连\n");
            		Class<?> c = MessageSend.class;
                    try {
    					Field connectFlag = c.getDeclaredField("connectFlag");
    					connectFlag.setAccessible(true);
    					connectFlag.setBoolean(connectFlag, false);
    					
    				} catch (NoSuchFieldException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				} catch (SecurityException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				} catch (IllegalArgumentException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				} catch (IllegalAccessException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
            	}
            	
            	
            		
            }});
		
		
	}
}
